package ar.pmt.vista.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import ar.pmt.vista.utils.Utils;

public class SessionControl implements Filter {

   /**
    * doFilter
    */
   public void doFilter(ServletRequest request, ServletResponse response,
         FilterChain chain) throws ServletException, IOException {

      HttpServletRequest myRequest = null;

      if (request instanceof HttpServletRequest) {
         myRequest = (HttpServletRequest)request;

         boolean expiro = Utils.checkSessionExpire(myRequest);
         boolean inicio = (myRequest.getRequestURL().indexOf("login") > 0 ||
                           myRequest.getRequestURL().indexOf("Login") > 0 ||
                           myRequest.getRequestURL().indexOf("logout") > 0);

          // Control de la Session
          if (expiro && !inicio) {
             myRequest.getSession().
             getServletContext().getRequestDispatcher("/jsp/invalidSession.jsp").forward(request, response);
             //request.getRequestDispatcher(request.get"/invalidSession.jsp");

          } else
             chain.doFilter(request, response);

      } else {
         chain.doFilter(request, response);
      }

   }

   public void init(FilterConfig config) throws ServletException {
   }

   public void destroy() {
   }

}